﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using FinPlusInterfaces;
using FinPlusCompCore;

namespace FinPlusCompProxy
{
    public class DepoRate : IRate
    {
        public double Value { get { return _level.Value; } }
        public int Shock { get; set; }
        public string Instrument { get { return _instrumentName; } }

        private enum Params { ShockUnit, Maturity, SettlementDays, DayCount, BizConv, Holidays };
        private FinPlusLevel _level;
        private QLAnalytics _analytics;
        private IPod _instrumentBuild, _instrument;
        private string _instrumentName, _tenor, _dayCount, _bizConv, _holidays;
        private double _shockUnit;
        private int _fixingDays;

        public DepoRate(QLAnalytics analytics, string instrumentName, IPod instrument, IPod instrumentBuild)
        {
            _analytics = analytics;
            _instrumentName = instrumentName;
            _instrument = instrument;
            _instrumentBuild = instrumentBuild;

            _level = new FinPlusLevel(analytics.BuySell);
            _tenor = _instrumentBuild[Params.Maturity.ToString()].ToString();
            _fixingDays = (int)_instrumentBuild.Value(Params.SettlementDays.ToString());
            _dayCount = _instrumentBuild[Params.DayCount.ToString()].ToString();
            _bizConv = _instrumentBuild[Params.BizConv.ToString()].ToString();
            _holidays = _instrumentBuild[Params.Holidays.ToString()].ToString();
            _shockUnit = (double)_instrument.Get(Params.ShockUnit.ToString()).Object;
        }

        public string Build(string buySell)
        {
            lock (_analytics.QL)
                return _analytics.QL.DepoRate(_analytics.MarketName, _instrumentName, _level.Value + _shockUnit * Shock, _tenor, _fixingDays, _dayCount, _bizConv, _holidays);
        }

        public bool Update(IPod level)
        {
            return _level.LevelUpdate(level);
        }
    }
}
